package com.web.reply;

import com.bean.Remark;
import com.bean.Reply;
import com.service.RemarkService;
import com.service.ReplyService;
import com.tools.SensitiveWordUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

public class AddReplyServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Reply reply = new Reply();
        ReplyService service = new ReplyService();

        HttpSession session = req.getSession();

        try {
            BeanUtils.populate(reply, req.getParameterMap());
            int remarkId=reply.getRemarkId();
            session.setAttribute("remarkId",remarkId);

            String content=reply.getContent();
            session.setAttribute("orReply",content);//原始回复


            //敏感词过滤
            SensitiveWordUtils sensitiveWordUtils=new SensitiveWordUtils();
            String reContent=sensitiveWordUtils.replaceSensitiveWord(content,2);
            //System.out.println(reContent);
            reply.setReContent(reContent);
            req.getSession().setAttribute("reContent",reContent);//过滤后评论
           // System.out.println(reply);

            service.addReply(reply);
            req.getRequestDispatcher("/DisplayReply").forward(req,resp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
